import java.sql.*;

/**
 * Created by Taller on 2017/8/8.
 */
public class Coding {
    public static String tableName = "user_info";
    public static int businessId = 101;

    public static void main(String[] args){
        String ku = "scf_base";
        //String url = "jdbc:mysql://172.16.0.213:3306/"+ku+"?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&useSSL=true";
        String url = "jdbc:mysql://192.168.3.250:3306/"+ku+"?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&useSSL=false";

        String user = "root";
        //String password = "root";

        String password = "Taller10086!";


        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);

            PreparedStatement pstm = conn.prepareStatement("select COLUMN_name,data_type from information_schema.columns where table_name = '"+tableName+"' AND table_schema='"+ku+"' ");

            ResultSet rs = pstm.executeQuery();

            System.out.println("===========================================");
            System.out.println("==================Model====================");
            System.out.println("===========================================");
            System.out.println("" +
                    "import com.baomidou.mybatisplus.activerecord.Model;\n" +
                    "import com.baomidou.mybatisplus.annotations.TableName;\n" +
                    "import com.xtaller.core.helper.IdHelper;\n" +
                    "import java.io.Serializable;\n" +
                    "@SuppressWarnings(\"serial\")\n" +
                    "@TableName(value = \""+tableName+"\")");
            System.out.println("public class "+formatName(tableName)+" extends Model<"+formatName(tableName)+"> {");
            while (rs.next()) {
                String init = "";
                if(rs.getString(1).equals("id")){
                    init = " = IdHelper.getId("+businessId+")";
                }
                System.out.println(" private "+ formatType(rs.getString(2))+" "+rs.getString(1) + init+";" );
            }
            System.out.println("@Override\n" +
                    "    protected Serializable pkVal() {\n" +
                    "        return id;\n" +
                    "    } \n" +
                    "}");
            System.out.println("===========================================");
            System.out.println("==================Mapper===================");
            System.out.println("===========================================");
            System.out.println("public interface "+formatName(tableName)+"Mapper extends BaseMapper<"+formatName(tableName)+"> {\n" +
                    "}");

            System.out.println("===========================================");
            System.out.println("==================IService=================");
            System.out.println("===========================================");
            System.out.println("public interface I"+formatName(tableName)+"Service {\n" +
                    "}");

            System.out.println("===========================================");
            System.out.println("==================Service=================");
            System.out.println("===========================================");
            System.out.println("@Service\n" +
                    "public class "+formatName(tableName)+"Service extends BaseServiceImpl<"+formatName(tableName)+"Mapper,"+formatName(tableName)+"> implements I"+formatName(tableName)+"Service {\n" +
                    "}");
            System.out.println("===========================================");
            System.out.println("==================Controller===============");
            System.out.println("===========================================");
            System.out.println("@RestController\n" +
                    "@RequestMapping(value = \""+formatServiceName(tableName)[0]+"\")");
            System.out.println("public class "+formatName(tableName)+"Controller {\n" +
                    "    @Autowired\n" +
                    "    private "+formatName(tableName)+"Service "+formatServiceName(tableName)[1]+"Service;\n" +
                    "\n" +
                    "    //增\n" +
                    "    @PostMapping(\"/v1\")\n" +
                    "    public R add(@RequestBody "+formatName(tableName)+" model){\n" +
                    "        if("+formatServiceName(tableName)[1]+"Service.insert(model))\n" +
                    "           return R.ok(\"success\"); \n" +
                    "        return R.error(\"error\");\n"+
                    "    }\n" +
                    "    //删\n" +
                    "    @DeleteMapping(\"/{id}/v1\")\n" +
                    "    public R delete(@PathVariable(\"id\") String id){\n" +
                    "        if("+formatServiceName(tableName)[1]+"Service.delete(id))\n" +
                    "           return R.ok(\"success\"); \n" +
                    "        return R.error(\"error\");\n"+
                    "    }\n" +
                    "    //改\n" +
                    "    @PutMapping(\"/v1\")\n" +
                    "    public R update(@RequestBody "+formatName(tableName)+" model){\n" +
                    "        if("+formatServiceName(tableName)[1]+"Service.update(model))\n" +
                    "           return R.ok(\"success\"); \n" +
                    "        return R.error(\"error\");\n"+
                    "    }\n" +
                    "    //查\n" +
                    "    @GetMapping(\"/{id}/v1\")\n" +
                    "    public R get(@PathVariable(\"id\") String id){\n" +
                    "        return R.ok("+formatServiceName(tableName)[1]+"Service.selectByIds(id));\n" +
                    "    }\n" +
                    "    //列表\n" +
                    "    @PostMapping(\"/{index}-{size}/v1\")\n" +
                    "    public R list(@PathVariable(\"index\") int index,\n" +
                    "                        @PathVariable(\"size\") int size,\n" +
                    "                        @RequestBody List<Where> where){\n" +
                    "        return R.ok("+formatServiceName(tableName)[1]+"Service.page(index,size,where));\n" +
                    "    }\n" +
                    "    //唯一性检查\n" +
                    "    @PostMapping(\"/exist/{type}/{attr}/v1\")\n" +
                    "    public R exist(@PathVariable(\"type\") int type,\n" +
                    "                          @PathVariable(\"attr\") String[] attr,\n" +
                    "                          @RequestBody "+formatName(tableName)+" model){\n" +
                    "        if("+formatServiceName(tableName)[1]+"Service.isExist(type,attr,model))\n" +
                    "           return R.ok(\"exist\"); \n" +
                    "        return R.error(\"unexist\");\n"+
                    "    }\n" +
                    "    //一般查询\n" +
                    "    @PostMapping(\"/query/v1\")\n" +
                    "    public R query(@RequestBody List<Where> where){\n" +
                    "        return R.ok("+formatServiceName(tableName)[1]+"Service.query(where));\n" +
                    "    }\n"+
                    "    //数据版本控制\n" +
                    "    @PostMapping(\"/version/v1\")\n" +
                    "    public R version(@RequestBody "+formatName(tableName)+" model){\n" +
                    "        "+formatName(tableName)+" obj = "+formatServiceName(tableName)[1]+"Service.selectById(model.getId());\n" +
                    "        if(obj.getVersion().toString().equals(model.getVersion().toString())) \n" +
                    "           return R.ok(\"success\"); \n" +
                    "        return R.error(\"error\");\n"+
                    "    } \n"+
                    "    //根据id判断数据是否存在\n" +
                    "    @GetMapping(\"/has/{id}/v1\")\n" +
                    "    public R has(@PathVariable(\"id\") String id){\n" +
                    "        if("+formatServiceName(tableName)[1]+"Service.selectCount(\n" +
                    "                new EntityWrapper<"+formatName(tableName)+">()\n" +
                    "                        .eq(\"isDel\",0)\n" +
                    "                        .eq(\"id\",id))>0)\n" +
                    "           return R.ok(\"success\"); \n" +
                    "        return R.error(\"error\");\n"+
                    "    }"+
                    "\n" +
                    "}");

            System.out.println("===========================================");
            System.out.println("===============Consumer Client============");
            System.out.println("===========================================");
            System.out.println("@FeignClient(value = \"LABS-GATEWAY\")\n" +
                    "public interface "+formatName(tableName)+"Client {\n" +
                    "    String path = \"/服务名称"+formatServiceName(tableName)[0]+"\";\n" +
                    "    //增\n" +
                    "    @PostMapping(path + \"/v1\")\n" +
                    "    R add(@RequestBody "+formatName(tableName)+" model);\n" +
                    "    //删\n" +
                    "    @DeleteMapping(path + \"/{id}/v1\")\n" +
                    "    R delete(@PathVariable(\"id\") String id);\n" +
                    "    //改\n" +
                    "    @PutMapping(path + \"/v1\")\n" +
                    "    R update(@RequestBody "+formatName(tableName)+" model);\n" +
                    "    //查\n" +
                    "    @GetMapping(path + \"/{id}/v1\")\n" +
                    "    R get(@PathVariable(\"id\") String id);\n" +
                    "    //列表\n" +
                    "    @PostMapping(path + \"/{index}-{size}/v1\")\n" +
                    "    R list(@PathVariable(\"index\") int index, @PathVariable(\"size\") int size, @RequestBody List<Where> where);\n" +
                    "    //唯一性检查\n" +
                    "    @PostMapping(path + \"/exist/{type}/{attr}/v1\")\n" +
                    "    R exist(@PathVariable(\"type\") int type, @PathVariable(\"attr\") String[] attr, @RequestBody "+formatName(tableName)+" model);\n" +
                    "    //一般查询\n" +
                    "    @PostMapping(path + \"/query/v1\")\n" +
                    "    R query(@RequestBody List<Where> where);\n"+
                    "    //版本控制\n" +
                    "    @PostMapping(path + \"/version/v1\")\n" +
                    "    R version(@RequestBody "+formatName(tableName)+" model);\n"+
                    "    //根据id判断数据是否存在\n" +
                    "    @GetMapping(path + \"/has/{id}/v1\")\n" +
                    "    R has(@PathVariable(\"id\") String id);\n"+
                    "}");

            rs.close();
            pstm.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static String formatType(String db){
        String type = "String";
        if(db.equals("bigint")){
            type = "String";
        }
        if(db.equals("tinyint")){
            type = "Integer";
        }
        if(db.equals("int")){
            type = "Integer";
        }

        return type;
    }

    private static String formatName(String name){
        String[] item = name.split("_");
        int len = item.length;
        String fileName = "";
        for(int i=0;i<len;i++){
            fileName += item[i].substring(0, 1).toUpperCase() + item[i].substring(1);
        }
        return fileName;
    }
    private static String[] formatServiceName(String name){
        String[] newName = new String[2];
        String[] item = name.split("_");
        int len = item.length;
        String n1 = "";
        String n2 = "";
        for(int i=0;i<len;i++){
            n1 += item[i]+"-";
            if(i>1){
                n2 += item[i].substring(0, 1).toUpperCase() + item[i].substring(1);
            }else {
                n2 += item[i];
            }
        }
        n1 = n1.substring(0,n1.length()-1);
        newName[0] = "/"+n1;
        newName[1] = n2;
        return newName;
    }
}
